const { Perms } = require('../Validation/presmissions.js');
const { Client } = require('discord.js');
const { MAIN_GUILD } = require('../config.json');

/**
 * @param {Client} client
 */

module.exports = async (client, PG, Ascii) => {
  const Table = new Ascii('Command Loaded');

  commandsArray = [];

  (await PG(`${process.cwd().replace(/\\/g, '/')}/Commands/*/*.js`)).map(
    async file => {
      const command = require(file);

      if (!command.name) {
        return Table.addRow(file.split('/')[6], '❌ FAILED', 'Missing a name.');
      }

      if (!command.context && !command.description)
        return Table.addRow(
          command.name,
          '❌ FAILED',
          'Missing a description.'
        );
      if (command.permission) {
        if (Perms.includes(command.permission)) {
          command.defauldPermission = false;
        } else
          return Table.addRow(
            command.name,
            '❌ FAILED',
            'Permission is invalid.'
          );
      }

      client.commands.set(command.name, command);
      commandsArray.push(command);

      await Table.addRow(command.name, '✔ SUCCESSFUL');
    }
  );

  console.log(Table.toString());

  // PERMISSINS CHECK

  client.on('ready', async () => {
    const mainGuild = await client.guilds.cache.get(MAIN_GUILD);
    mainGuild.commands.set(commandsArray);
  });
};
